<template>
  <div>
    <div>
      <FormPanel ref="formPanel" :list="formList" :cols="2" :labelWidth="110" :formType="type" :isSubmitBtn="false" @formChange="formChangeHandle" />
    </div>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <el-button @click="closeDialog">取 消</el-button>
      <el-button v-if="type !== 'show'" :loading="confirmBtnLoading" type="primary" @click="saveDialog">{{ btnText }}</el-button>
    </div>
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import { addStorage, updateStorage } from '@/api/part/Warehouse';

export default {
  name: 'BrandModal',
  mixins: [dictionary],
  props: {
    type: {
      type: String,
      default: 'add'
    },
    rowData: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    this.conf = { add: '保 存', edit: '修 改' };
    this.saveData = null;
    this.flag = false;
    return {
      formList: this.createFormList(),
      confirmBtnLoading: false
    };
  },
  computed: {
    btnText() {
      return this.conf[this.type];
    }
  },
  created() {
    this.init();
  },
  methods: {
    init() {
      if (this.type === 'edit') {
        // 更新接口需要的记录 ID
        this.recordId = this.rowData.storageCode;
        console.log(this.rowData.stopDate);
        this.formList.forEach(x => {
          x.initialValue = this.rowData[x.fieldName];
        });
        this.formList.find(item => item.fieldName == 'storageCode').disabled = true;
        this.formList.find(item => item.fieldName == 'oemTag').disabled = true;
        this.formList.find(item => item.fieldName == 'storageName').disabled = this.rowData['oemTag'] == '10451002';
        this.formList.find(item => item.fieldName == 'storageType').disabled = true;
        this.formList.find(item => item.fieldName == 'pricingMethod').disabled = true;
        // this.formList.find(item => item.fieldName == 'stopDate').initialValue = this.rowData.workshopTag == '10041001' ? '' : new Date();
      }
    },
    createFormList() {
      return [
        {
          type: 'INPUT',
          label: '仓库代码',
          fieldName: 'storageCode',
          placeholder: '请输入...',
          rules: [
            { required: true, message: '请输入仓库代码', trigger: 'blur' }
            // { min: 1, max: 4, message: '长度在 1 到 4 个字符', trigger: 'blur' }
          ]
        },
        {
          type: 'INPUT',
          label: '仓库名称',
          fieldName: 'storageName',
          placeholder: '请输入...',
          rules: [
            { required: true, message: '请输入仓库名称', trigger: 'blur' },
            { min: 1, max: 8, message: '长度在 1 到 8 个字符', trigger: 'blur' }
          ]
        },
        {
          type: 'SELECT',
          label: '仓库类型',
          fieldName: 'storageType',
          placeholder: '请选择',
          itemList: this.createDictList('5000'),
          rules: [{ required: true, message: '请选择仓库类型', trigger: 'change' }]
        },
        {
          type: 'SELECT',
          label: '是否有效',
          fieldName: 'workshopTag',
          placeholder: '请选择',
          initialValue: 10041001,
          itemList: this.createDictList('1004'),
          change: e => {
            // if (this.type === 'edit') {
            //   this.formList.find(item => item.fieldName == 'stopDate').initialValue = e == '10041001' ? new Date() : '';
            // } else {
            this.formList.find(item => item.fieldName == 'stopDate').initialValue = e == '10041001' ? '' : new Date();
            // }
          }
        },
        {
          type: 'DATE',
          label: '停用时间',
          fieldName: 'stopDate',
          disabled: true,
          valueFormat: 'yyyy-MM-dd'
          // rules: [{ required: true, message: '请选择日期', trigger: 'change' }]
        },
        {
          type: 'SELECT',
          label: '数据来源',
          fieldName: 'oemTag',
          placeholder: '请选择',
          disabled: true,
          itemList: this.createDictList('1045'),
          initialValue: this.createDictList('1045')[0].value,
          // itemList: [{text:'店面',value:'10451001'},{text:'主机厂',value:'10451002'}],
          rules: [{ required: false, message: '请选择数据来源', trigger: 'change' }]
        },
        {
          type: 'SELECT',
          label: '计价方式',
          fieldName: 'pricingMethod',
          placeholder: '请选择',
          initialValue: 80851001,
          disabled: true,
          itemList: this.createDictList('8085'),
          // itemList: [{text:'店面',value:'10451001'},{text:'主机厂',value:'10451002'}],
          rules: [{ required: true, message: '请选择计价方式', trigger: 'change' }]
        }
      ];
    },
    formChangeHandle(val) {
      // this.saveData = val;
      // if (this.saveData !== null) {
      this.saveHandle(val);
      // }
    },
    async saveHandle(val) {
      if (this.confirmBtnLoading) return;
      this.confirmBtnLoading = true;
      let res = {};
      if (this.type === 'add') {
        res = await addStorage(val);
      }
      if (this.type === 'edit') {
        res = await updateStorage(val, this.recordId);
      }
      this.confirmBtnLoading = false;
      if (res.resultCode == '200') {
        this.test();
      }
      this.$emit('refeshTableData');
    },
    async saveDialog() {
      this.$refs.formPanel.SUBMIT_FORM();
    },
    checkStorageCode(rule, value, callback) {
      if (!value) {
        return callback(new Error(rule.message));
      } else {
        if (/^[A-Za-z0-9]+$/.test(value)) {
          callback();
        } else {
          return callback(new Error(rule.message));
        }
      }
      callback();
    },
    closeDialog(param) {
      this.$emit('close', false, param);
    },
    test() {
      this.$notify({
        title: '成功',
        message: '成功',
        type: 'success'
      });
      this.closeDialog('refresh');
    }
  }
};
</script>

<style lang="less" scoped></style>
